package com.azkj.album.view.activity;

import android.os.Build;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.azkj.album.R;
import com.azkj.album.view.base.BaseActivity;
import com.azkj.album.view.widgets.TitleNavBar;
import com.azkj.album.view.widgets.dialog.ShareDialog;

import butterknife.BindView;

public class WebViewActivity extends BaseActivity {

    @BindView(R.id.titleBar)
    TitleNavBar mTitleBar;

    @BindView(R.id.webView)
    WebView mWebview;

    @BindView(R.id.pb_web_activity)
    ProgressBar mProgressBar;


    private String shareTiTle, shareDesc, shareUrl;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_webview;
    }

    @Override
    public void initView() {


        mWebview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);  //设置 缓存模式(true);
        mWebview.getSettings().setSupportZoom(false);
        mWebview.getSettings().setUseWideViewPort(true);
        mWebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        mWebview.getSettings().setAllowFileAccess(true);
        // 设置是否允许通过 file url 加载的 Javascript 可以访问其他的源(包括http、https等源)
        mWebview.getSettings().setAllowUniversalAccessFromFileURLs(true);
        mWebview.getSettings().setAllowContentAccess(true);
        mWebview.getSettings().setDomStorageEnabled(true);
        mWebview.getSettings().setDatabaseEnabled(true);
        mWebview.getSettings().setJavaScriptEnabled(true);
        mWebview.setWebChromeClient(new WebChromeClient());
        mWebview.setWebViewClient(new WebViewClient());
        mWebview.getSettings().setPluginState(WebSettings.PluginState.ON);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mWebview.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

    }

    @Override
    public void initData() {
        mTitleBar.setLeftListener(v -> finish());
        mTitleBar.setTitle(getIntent().getStringExtra("title"));
        shareTiTle = getIntent().getStringExtra("shareTiTle");
        shareDesc = getIntent().getStringExtra("shareDesc");
        shareUrl = getIntent().getStringExtra("url");

        if (getIntent().getBooleanExtra("isShowShare", false)) {
            mTitleBar.getRightIv().setVisibility(View.VISIBLE);
            mTitleBar.getRightIv().setImageResource(R.mipmap.ic_share);
        }

        mWebview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView webView, int i) {

                mProgressBar.setVisibility(View.VISIBLE);
                mProgressBar.setProgress(i);
                if (i == 100) {
                    mProgressBar.setVisibility(View.GONE);
                }
                super.onProgressChanged(webView, i);
            }
        });


        mWebview.loadUrl(shareUrl);

        mTitleBar.getRightIv().setOnClickListener(v -> {

            new ShareDialog.Builder(this).setCallBack(new ShareDialog.Builder.ItemClickListener() {
                @Override
                public void onFriendClick() {
                    shareWeb(false, shareUrl, shareTiTle, shareDesc);
                }

                @Override
                public void onZoneClick() {
                    shareWeb(true, shareUrl, shareTiTle, shareDesc);
                }
            }).show();


        });

    }


    @Override
    protected void initPresenter() {

    }

}
